package com.wu.foodsafetyplayer.app.fragment.main;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;

import com.tencent.smtt.export.external.interfaces.WebResourceError;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.wu.foodsafetyplayer.Application;
import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.desbridgewebview.MyWebView;
import com.wu.foodsafetyplayer.app.dialog.LoadingUtil;
import com.wu.foodsafetyplayer.app.fragment.BaseFragment;


public class SafetyFragment extends BaseFragment implements View.OnClickListener {
    MyWebView webView;
    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_safetyl;
    }

    @Override
    protected void initView() {
        super.initView();
        webView = findViewById(R.id.f_safetyl_web);
        initWebView();
//        webView.getSettings().setJavaScriptEnabled(true);
//        webView.getSettings().setAppCacheEnabled(true);
//        //设置 缓存模式
//        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//        // 开启 DOM storage API 功能
//        webView.getSettings().setDomStorageEnabled(true);
//        webView.setWebViewClient(new WebViewClient() {
//            //覆盖shouldOverrideUrlLoading 方法
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });
        webView.loadUrl("http://www.syxwnet.com/hyzl/p/162324.html");
        LoadingUtil.INSTANCE.showLoading(getActivity(),"加载中");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

    private void initWebView(){
        webView.setHorizontalScrollBarEnabled(false);
        webView.setHorizontalFadingEdgeEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.setVerticalFadingEdgeEnabled(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setDisplayZoomControls(true);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setCacheMode(-1);
        webView.getSettings().setAppCachePath(Application.getAppContext().getCacheDir().getAbsolutePath());
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setNeedInitialFocus(true);
        webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
                Log.e("pageTAGs","start---");
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                Log.e("pageTAGs","finish---");
                LoadingUtil.INSTANCE.dismissLoading(getActivity());
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
                Log.e("pageTAGs","---:" + i);
            }

            @Override
            public void onReceivedTitle(WebView webView, String s) {
                super.onReceivedTitle(webView, s);
            }
        });
    }
}
